package com.chandra.pos.employee.dao;

import com.chandra.pos.SearchCriteria;
import com.chandra.pos.employee.dao.entity.Employee;
import com.chandra.pos.employee.dao.entity.EmployeeRole;
import com.chandra.pos.employee.exception.SystemUserNotFoundException;
import com.chandra.pos.employee.model.EmployeeSearchCriteria;
import com.chandra.pos.employee.repository.EmployeeRepository;
import com.chandra.pos.employee.repository.EmployeeRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.inject.Named;
import java.util.List;

/**
 * 
 * Customer DAO
 * 
 * @author onlinetechvision.com
 * @since 25 Mar 2012
 * @version 1.0.0
 *
 */

@Named
public class SystemUserDAO implements ISystemUserDAO {



    @Autowired
    private EmployeeRepository employeeRepository;


    @Autowired
    private EmployeeRoleRepository employeeRoleRepository;

    /**
	 * Add Customer
	 * 
	 * @param  customer customer
	 */
	@Override
	public Employee addSystemUser(Employee customer) {
        return employeeRepository.save(customer);
	}

	/**
	 * Delete Customer
	 * 
	 * @param  customer customer
	 */
	@Override
	public void deleteUser(Employee customer) {
        employeeRepository.delete(customer);
	}

	/**
	 * Update Customer
	 * 
	 * @param  User customer
	 */
	@Override
	public Employee updateUser(Employee customer) {
        return  employeeRepository.save(customer);
	}

	/**
	 * Get Customer
	 * 
	 * @param  int Customer Id
	 * @return Customer
	 */
	@Override
	public Employee getUserById(Long id) {

       return employeeRepository.findOne(id);


	}

	/**
	 * Get Customer List
	 * 
	 * @return List - Customer list
	 */
	@Override
	public Page getUsers(PageRequest posPagination) {


       Page list = employeeRepository.findAll(posPagination);
		return list;
	}

    @Override
    public Employee getUserDetail(String firstName, String lastName, String city) throws SystemUserNotFoundException {
        return employeeRepository.findByFirstNameAndLastNameAndCity(firstName,lastName,city);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Employee getUserDetail(Long id) throws SystemUserNotFoundException {
        return employeeRepository.findOne(id);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Employee getUserDetail(String userName) throws SystemUserNotFoundException {
        Employee employee = employeeRepository.findByUserName(userName) ;

        if(employee ==null)
            throw new  SystemUserNotFoundException();

        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Employee> searchUsers(EmployeeSearchCriteria searchCriteria) {
        return employeeRepository.search(searchCriteria);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Employee> searchUsers(SearchCriteria searchCriteria) {
        return employeeRepository.search(searchCriteria);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<EmployeeRole> getUserRoles(Long id) {
        return employeeRoleRepository.findByEmployeeId(id);  //To change body of implemented methods use File | Settings | File Templates.
    }

}
